package cn.edu.scau.cmi.lixiaoyuan.orm.hibernate.dao;

import java.util.List;

import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import cn.edu.scau.cmi.lixiaoyuan.orm.hibernate.domain.Student;
import cn.edu.scau.cmi.lixiaoyuan.orm.hibernate.domain.Teacher;
@Repository
public class TeacherDAO extends BaseHibernateDAO{
	private static final Logger log = LoggerFactory.getLogger(TeacherDAO.class);
	public void save(Teacher transientInstance) throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		log.debug("saving Teacher instance");
		try {
			Session session = getSession();
			Transaction ts = (Transaction) session.beginTransaction();
			session.save(transientInstance);
			ts.commit();
			session.close();
			log.debug("save successful");
		}catch(RuntimeException re) {
			log.error("save failed",re);
			throw re;
		}
	}
	
	public void delete(Teacher transientInstance) throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		log.debug("deleting Teacher instance");
		try {
			Session session = getSession();
			Transaction ts = (Transaction) session.beginTransaction();
			session.delete(transientInstance);
			ts.commit();
			session.close();
			log.debug("delete successful");
		}catch(RuntimeException re) {
			log.error("delete failed",re);
			throw re;
		}
	}
	
	public void update(Teacher transientInstance) throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		log.debug("updating Teacher instance");
		try {
			Session session = getSession();
			Transaction ts = (Transaction) session.beginTransaction();
			session.update(transientInstance);
			ts.commit();
			session.close();
			
			log.debug("update successful");
		}catch(RuntimeException re) {
			log.error("update failed",re);
			throw re;
		}
	}
	
	public Teacher findById(java.lang.Long id) throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		log.debug("getting Teacher instance with id: " + id);
		try {
			Session session = getSession();
			Transaction ts = (Transaction) session.beginTransaction();
			Teacher instance = (Teacher)session.get("cn.edu.scau.cmi.liangjiale.orm.hibernate.domain.Teacher",id);
			ts.commit();
			session.close();
			
			log.debug("delete successful");
			return instance;
			
		}catch(RuntimeException re) {
			log.error("delete failed",re);
			throw re;
		}
	}
	
	public List<Teacher> findByProperty(String propertyName, Object value) throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		log.debug("finding Teacher instance with property: " + propertyName + ", value: " + value);
		try {
			Session session = getSession();
			Transaction ts = (Transaction) session.beginTransaction();
			String queryString = "from Teacher as model where model." + propertyName + "= ?";
			Query queryObject = session.createQuery(queryString);
			queryObject.setParameter(0, value);
			ts.commit();
			List<Teacher> list = queryObject.list();
			session.close();
			return list;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List<Teacher> findByName(Object name) throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException{
		return findByProperty("name",name);
	}
	
	
	public Teacher merge(Teacher detachedInstance) {
		log.debug("merging Teacher instance");
		try {
			Session session = getSession();
		Transaction ts = (Transaction) session.beginTransaction();
		Teacher result = (Teacher)session.merge(detachedInstance);
		ts.commit();
		session.close();
			
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	
	public List<Teacher> findAll(){
		{
			log.debug("finding all Teacher instances");
			try {
				Session session = getSession();
				Transaction ts = (Transaction) session.beginTransaction();
				String queryString = "from Teacher";
				Query queryObject = getSession().createQuery(queryString);
				ts.commit();
				List<Teacher> list = queryObject.list();
				session.close();
				return list;
			} catch (RuntimeException re) {
				log.error("find all failed", re);
				throw re;
			}
		}
	}
}
